export default {
  // 加密函数(AES)
  encrypt (bytes) {
    return bytes
  },
  // 解密函数(AES)
  decrypt (bytes) {
    return bytes
  },
  // encode函数
  encode (protoId, data) {
    let pkgShell = {}
    pkgShell.pid = protoId
    pkgShell.data = this.encrypt(JSON.stringify(data))
    return JSON.stringify(pkgShell)
  },
  // decode函数
  decode (jsonStr) {
    return jsonStr
  },

  // 时间戳转字符串
  getDate (dateTimeStamp) {
    let d = new Date(dateTimeStamp * 1000)
    let date = (d.getFullYear()) + '-' +
      (d.getMonth() + 1) + '-' +
      (d.getDate()) + ' ' +
      (d.getHours()) + ':' +
      (d.getMinutes()) + ':' +
      (d.getSeconds())
    return date
  },
  // 时间格式函数
  getDateDiff (dateTimeStamp) {
    let result = ''
    let minute = 1000 * 60
    let hour = minute * 60
    let day = hour * 24
    let month = day * 30
    let year = day * 365
    let now = new Date().getTime()
    let diffValue = now - dateTimeStamp * 1000
    if (diffValue < 0) {
      return '刚刚'
    }
    let yearC = diffValue / year
    let monthC = diffValue / month
    let weekC = diffValue / (7 * day)
    let dayC = diffValue / day
    let hourC = diffValue / hour
    let minC = diffValue / minute
    if (yearC >= 1) {
      result = parseInt(yearC) + '年前'
    } else if (monthC >= 1) {
      result = parseInt(monthC) + '个月前'
    } else if (weekC >= 1) {
      result = parseInt(weekC) + '周前'
    } else if (dayC >= 1) {
      result = parseInt(dayC) + '天前'
    } else if (hourC >= 1) {
      result = parseInt(hourC) + '个小时前'
    } else if (minC >= 1) {
      result = parseInt(minC) + '分钟前'
    } else {
      result = '刚刚'
    }
    return result
  },

  // 当前时间
  timeNow () {
    var myDate = new Date()
    myDate.getYear() // 获取当前年份(2位)
    myDate.getFullYear() // 获取完整的年份(4位,1970-????)
    myDate.getMonth() // 获取当前月份(0-11,0代表1月)
    myDate.getDate() // 获取当前日(1-31)
    myDate.getDay() // 获取当前星期X(0-6,0代表星期天)
    myDate.getTime() // 获取当前时间(从1970.1.1开始的毫秒数)
    myDate.getHours() // 获取当前小时数(0-23)
    myDate.getMinutes() // 获取当前分钟数(0-59)
    myDate.getSeconds() // 获取当前秒数(0-59)
    myDate.getMilliseconds() // 获取当前毫秒数(0-999)
    myDate.toLocaleDateString() // 获取当前日期
    return myDate.toLocaleString() // 获取日期与时间
  },
  // 时间戳转字符串
  formatDate (date) {
    var YY = date.getFullYear() + '-'
    var MM = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
    var DD = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate())
    // var hh = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':'
    // var mm = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':'
    // var ss = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds())
    return YY + MM + DD
    // return YY + MM + DD + ' ' + hh + mm + ss
  },
  // base64转二进制
  dataURLToBlob (dataurl) {
    let arr = dataurl.split(',')
    let mime = arr[0].match(/:(.*?);/)[1]
    let bstr = atob(arr[1])
    let n = bstr.length
    let u8arr = new Uint8Array(n)
    while (n--) {
      u8arr[n] = bstr.charCodeAt(n)
    }
    return new Blob([u8arr], { type: mime })
  },
  // 计算楼层
  getFloor (num) {
    if (num === 1) {
      return '沙发'
    }
    if (num === 2) {
      return '板凳'
    }
    return num + '楼'
  },
  // 随机数
  randomNum (min, max) {
    return Math.floor(Math.random() * (max - min) + min)
  }
}
